<template>
    <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="150px"
        class="demo-ruleForm"
    >
        <el-form-item label="请输入旧密码" prop="oldpass">
            <el-input
                type="oldpass"
                v-model="ruleForm.password"
                autocomplete="off"
            ></el-input>
        </el-form-item>
        <el-form-item label="请输入新密码" prop="newpass">
            <el-input
                type="newpass"
                v-model="ruleForm.password"
                autocomplete="off"
            ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input
                type="password"
                v-model.trim="ruleForm.checkPass"
                autocomplete="off"
            ></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    data() {
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.ruleForm.password) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                oldpass: '',
                newpass: '',
                checkPass: ''
            },
            rules: {
                oldpass: [{
                    required: true,
                    message: "不能为空"
                }],
                newpass: [
                    {
                        pattern: /^[a-zA-Z0-9_*]{6,50}$/,
                        message: "长度在 6 到 50 个字符",
                        trigger: "blur",
                        required: true,
                    },
                ],
                checkPass: [{validator: validatePass2, required: true, trigger: "blur"}],

            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.oldpass == JSON.parse(localStorage.getItem('userinfo')).userinfo.password) {
                        axios.post("/", this.ruleForm.newpass, {
                            headers: {
                                "Content-Type": "application/json",
                            },
                        }).then((resp) => {
                            const str = resp.data;
                            if (200 == parseInt(str.status)) {
                                localStorage.setItem("userinfo", JSON.stringify(str.data))
                                this.$alert("修改成功", {
                                    confirmButtonText: "确定",
                                    callback: (action) => {
                                        // this.$message({
                                        //   type: 'info',
                                        //   message: `action: ${ action }`
                                        // });
                                    },
                                });
                            } else {
                                alert("修改失败")
                                return false;
                            }
                        })
                    }
                }
            })
        }
    }

}
</script>

<style scoped>

</style>
